package com.Cfeng.XiaohuanChat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Cfeng
 * @date 2022/8/7
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //欢迎页面
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/login.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    //静态资源访问，虚拟路径配置，就可以访问虚拟盘,可以直接再yaml中配置locations即可
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("file:D:/softVM/XiaohuanChat/asserts/userImg/");
//    }


    /**
     * 显式声明失败的处理器
     * failureURL的底层就是SimpleUrlAuthenticationFailureHandler，设置重定向路径
     * forward对应的Forward
     * 放在这里为了避免循环
     */
    @Bean
    public SimpleUrlAuthenticationFailureHandler failureHandler() {
        SimpleUrlAuthenticationFailureHandler failureHandler = new SimpleUrlAuthenticationFailureHandler();
        //设置重定向路径,forward表示通过服务器端跳转来重新回到登录页面，所以不使用
        failureHandler.setDefaultFailureUrl("/logError.html");
        return failureHandler;
    }
}
